<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link rel="stylesheet" href="../../resource/frozen/css/frozen.css">
<script type="text/javascript" src="../../resource/js/zepto.min.js"></script>
<link rel="stylesheet" href="../../resource/css/shake.css">

<script src="../../resource/frozen/js/frozen.js"></script>
<script src="../../resource/js/result.js"></script>
<script src="../../resource/js/phoneTip.js"></script>
<script src="../../resource/frozen/js/frozen.js"></script>

<title>停车一道通</title>
<script type="text/javascript">
	//控制弹出提示框以后，不再执行摇一摇
	var flag = false;
	function onBridgeReady() {
		WeixinJSBridge.call('hideOptionMenu');
	}
	if (typeof WeixinJSBridge == "undefined") {
		if (document.addEventListener) {
			document.addEventListener('WeixinJSBridgeReady', onBridgeReady,
					false);
		} else if (document.attachEvent) {
			document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
			document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
		}
	} else {
		onBridgeReady();
	}

	window.onload = function() {
		init();
	}
	var SHAKE_THRESHOLD = 3000;//定义一个摇动的阈值
	var last_update = 0;//定义一个变量记录上一次摇动的时间
	var x = y = z = last_x = last_y = last_z = 0;//定义x、y、z记录三个轴的数据以及上一次触发的时间
	function init() {
		//判断移动浏览器是否支持运动传感器事件
		if (window.DeviceMotionEvent) {
			//添加一个事件监听器
			window.addEventListener('devicemotion', deviceMotionHandler, false);
		} else {
			frozen_info("not support mobile event");
		}
	}

	//运动传感器处理
	function deviceMotionHandler(eventData) {
		if(flag){
			windw.removeEventListener('devicemotion', deviceMotionHandler, false);
			return;
		}
		//获取含重力加速
		var acceleration = eventData.accelerationIncludingGravity;
		var curTime = new Date().getTime();//获取当前时间戳
		var diffTime = curTime - last_update;
		if (diffTime > 100) {
			last_update = curTime;//记录上一次摇动的时间
			x = acceleration.x;//获取加速度X方向
			y = acceleration.y;//获取加速度Y方向
			z = acceleration.z;//获取加速度垂直方向
			var speed = Math.abs(x + y + z - last_x - last_y - last_z)
					/ diffTime * 10000;//计算阈值
			if (speed > SHAKE_THRESHOLD) {
				//TODO 相关方法，比如：
				//播放音效
				shakeAudio.play();
				//播放动画
				$('.shake_box').addClass('shake_box_focus');
				clearTimeout(shakeTimeout);
				var shakeTimeout = setTimeout(function() {
					$('.shake_box').removeClass('shake_box_focus');
					shakeCheck();
				}, 1000)
			}
			//记录上一次加速度
			last_x = x;
			last_y = y;
			last_z = z;
		}
	}

	//预加摇一摇声音
	var shakeAudio = new Audio();
	shakeAudio.src = '../../resource/sound/shake_sound.mp3';
	var shake_options = {
		preload : 'auto'
	}
	for ( var key in shake_options) {
		if (shake_options.hasOwnProperty(key) && (key in shakeAudio)) {
			shakeAudio[key] = shake_options[key];
		}
	}

	function shakeCheck() {
		$.ajax({
			type : "post",
			url : linkPre + "/ydtu/shake/getCount.htm",
			dataType : "json",
			async : false,
			success : function(data) {
				checkResult_weixin(data.result);
				flag = true;
				if (data.result == SUCCESS) {
					shakeForCoupon();
				} else {
					$(".ui-dialog").dialog("show");
// 					frozen_info("一天只能获取一次");
				}
			}
		})
	}
	function shakeForCoupon() {
		$.ajax({
			type : "post",
			url : linkPre + "/ydtu/shake/insertCoupon.htm",
			dataType : "json",
			async : false,
			success : function(data) {
				checkResult_weixin(data.result);
				if (data.result == SUCCESS) {
					location.href = "shakeList.html";
				}
			}
		})
	}
</script>
</head>
<body ontouchstart>
<header class="ui-header ui-header-positive ui-border-b">
          <i class="ui-icon-return" onclick="closeWindow()"></i><h1>摇一摇</h1><button class="ui-btn" id="history">历史</button>
</header>
 <section class="ui-container">
		<div class="shake_box">
			<div class="shakTop"><span></span></div>
			<div class="shakBottom"><span></span></div>
		</div>
</section>
	<div class="ui-dialog">
		<div class="ui-dialog-cnt">
			<div class="ui-dialog-bd">
				<h3 style="color:black;font-weight:bold">提示</h3>
				<div style="text-align:center;color:black">一天只能获取一次</div>
			</div>
			<div class="ui-dialog-ft">
				<button type="button" id="sure" onclick="clo()">确定</button>
			</div>
		</div>
	</div>
<script>
$("#history").bind("click",function(){
	location.href="shakeList.html"
});

function clo() {
	$(".ui-dialog").dialog("hide");
// 	closeWindow();
}
</script>
</body>
</html>